package com.fmdj.dr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.dr.controller.form.*;
import com.fmdj.dr.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机端模块接口")
public class DriverController {
    @Resource
    private DriverService driverService;

    @PostMapping("/registerDriver")
    @Operation(summary = "司机注册接口")
    public CommonResult registerDriver(@RequestBody @Valid RegisterDriverForm form) {

        Map param = BeanUtil.beanToMap(form);
        String driverId = driverService.registerDriver(param);
        //返回给前端注册成功的ID
        return CommonResult.ok().put(CommonResult.RETURN_USER_ID, driverId);

    }


    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新司机实名认证信息接口")
    public CommonResult updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverAuth(stringObjectMap);
        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录")
    public CommonResult login(@RequestBody @Valid LoginForm form) {
        HashMap loginResult = driverService.login(form.getCode());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, loginResult);
    }

    @PostMapping("/selectDriverInfo")
    @Operation(summary = "查询司机个人信息")
    public CommonResult selectDriverInfo(@RequestBody @Valid SelectDriverInfoForm form) {
        HashMap hashMap = driverService.selectDriverInfo(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    /*
     * 查询司机实名认证信息接口
     * */
    @PostMapping("/selectDriverInfoById")
    @Operation(summary = "查询司机个人信息接口")
    public CommonResult selectDriverAuth(@RequestBody @Valid SelectDriverAuthForm form) {
        HashMap hashMap = driverService.selectDriverInfoById(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/selectDriverByPage")
    @Operation(summary = "查询司机分页记录")
    public CommonResult selectDriverByPage(@RequestBody @Valid SelectDriverByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = driverService.selectDriverByPage(param);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }

    /**
     * PC端司机实名认证信息查询
     */
    @PostMapping("/selectDriverAuthInfo")
    @Operation(summary = "PC端司机实名认证信息查询")
    public CommonResult selectDriverAuthInfo(@RequestBody @Valid SelectDriverAuthInfoForm form) {
        HashMap map = driverService.selectDriverAuthInfo(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, map);
    }

    @PostMapping("/updateDriverRealAuth")
    @Operation(summary = "更新司机实名认证状态")
    public CommonResult updateDriverRealAuth(@RequestBody @Valid UpdateDriverRealAuthForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverRealAuth(param);
        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }




}
